<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="数据包" prop="jhDataPackageId">
        <el-select v-model="queryParams.jhDataPackageId" filterable placeholder="请选择数据包">
          <el-option v-for="item in dataPackageList" :key="item.jhId" :label="item.jhDataName" :value="item.jhId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="店铺" prop="jhTmAccountId">
        <el-select v-model="queryParams.jhTmAccountId" filterable placeholder="请选择店铺">
          <el-option v-for="item in tmAccountList" :key="item.jhId" :label="item.jhName" :value="item.jhId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="jhName">
        <el-input v-model="queryParams.jhName" clearable placeholder="请输入商品名称" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品分类" prop="jhClass">
        <el-input v-model="queryParams.jhClass" clearable placeholder="请输入商品分类" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="上架日期" prop="jhListingTime">
        <el-date-picker v-model="queryParams.jhListingTime" clearable placeholder="请选择上架日期" type="date" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['jihuo:sale:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini" type="danger" @click="handleDelete">
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['jihuo:sale:export']" icon="el-icon-download" plain size="mini" type="warning" @click="handleExport">
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="saleList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column   key="jhDataPackageId" align="center" label="数据包" prop="jhDataPackageId">
        <template slot-scope="scope">
          <el-tag size="small">{{ dataPackageList.find(x => x.jhId == scope.row.jhDataPackageId) ? dataPackageList.find(x => x.jhId == scope.row.jhDataPackageId).jhDataName : '' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column   key="jhTmAccountId" align="center" label="天猫店铺" prop="jhTmAccountId">
        <template slot-scope="scope">
          <el-tag size="small">{{ tmAccountList.find(x => x.jhId == scope.row.jhTmAccountId) ? tmAccountList.find(x => x.jhId == scope.row.jhTmAccountId).jhName : '' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="商品名称" prop="jhName"/>
      <el-table-column align="center" label="商品分类" prop="jhClass"/>
      <el-table-column align="center" label="源商品地址" prop="jhOriginalUrl"/>
      <el-table-column align="center" label="上架日期" prop="jhListingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jhListingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="销量" prop="jhSalesVolume"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-hasPermi="['jihuo:sale:edit']" icon="el-icon-edit" size="mini" type="text" @click="handleUpdate(scope.row)">
            修改
          </el-button>
          <el-button v-hasPermi="['jihuo:sale:remove']" icon="el-icon-delete" size="mini" type="text" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList"/>

  </div>
</template>

<script>
import {addSale, delSale, getSale, listSale, updateSale} from "@/api/jihuo/productOnSale";
import {listData} from "@/api/jihuo/data";
import {listAccount} from "@/api/jihuo/account";

export default {
  name: "Sale",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      dataPackageList: [],
      tmAccountList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 在售商品表格数据
      saleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jhDataPackageId: null,
        jhItemId: null,
        jhTmAccountId: null,
        jhName: null,
        jhStatus: '2',
        jhClass: null,
        jhOriginalUrl: null,
        jhListingTime: null,
        jhSalesVolume: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询在售商品列表 */
    getList() {
      this.loading = true;
      listSale(this.queryParams).then(response => {
        this.saleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listData().then(response => {
        this.dataPackageList = response.rows;
      });
      listAccount().then(response => {
        this.tmAccountList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jhId: null,
        jhRevision: null,
        jhCreatedBy: null,
        jhCreatedTime: null,
        jhUpdatedBy: null,
        jhUpdatedTime: null,
        jhDataPackageId: null,
        jhItemId: null,
        jhTmAccountId: null,
        jhName: null,
        jhClass: null,
        jhOriginalUrl: null,
        jhListingTime: null,
        jhSalesVolume: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jhId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加在售商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const jhId = row.jhId || this.ids
      getSale(jhId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改在售商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.jhId != null) {
            updateSale(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSale(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jhIds = row.jhId || this.ids;
      this.$modal.confirm('是否确认删除在售商品编号为"' + jhIds + '"的数据项？').then(function () {
        return delSale(jhIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jihuo/sale/export', {
        ...this.queryParams
      }, `sale_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
